﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace SaturniLibrary.Web
{
    public class CookieManager
    {

        public static void SetCookies(string name, object value)
        {
            if (value != null)
            {
                HttpCookie httpCookie = new HttpCookie(name, value.ToString());
                httpCookie.Path = "/";
                HttpContext.Current.Response.Cookies.Add(httpCookie);
            }
        }
        public static void SetCookies(string name, object value, DateTime expiresTime)
        {
            if (value != null)
            {
                HttpCookie httpCookie = new HttpCookie(name, value.ToString());
                httpCookie.Path = "/";
                httpCookie.Expires = expiresTime;
                HttpContext.Current.Response.Cookies.Add(httpCookie);
            }
        }
        public static void SetMultiValuedCookies(string key, string name, object value, DateTime expiresTime)
        {
            HttpCookie httpCookie = HttpContext.Current.Request.Cookies[key];
            if (httpCookie == null)
            {
                httpCookie = new HttpCookie(key);
                httpCookie.Path = "/";
            }
            httpCookie[name] = value.ToString().Trim();
            httpCookie.Expires = expiresTime;
            HttpContext.Current.Response.Cookies.Add(httpCookie);
        }
        public static HttpCookie GetCookies(string name)
        {
            return HttpContext.Current.Request.Cookies[name];
        }
        public static string GetStringCookie(string name)
        {
            if (HttpContext.Current.Request.Cookies[name] != null)
            {
                return HttpContext.Current.Request.Cookies[name].Value;
            }
            return "";
        }
        public static string GetMultiValuedCookie(string key, string name)
        {
            HttpCookie httpCookie = HttpContext.Current.Request.Cookies[key];
            if (httpCookie != null) return httpCookie[name];
            return string.Empty;
        }
        public static void ClearCookie(string name)
        {
            SetCookies(name, string.Empty, DateTime.Now.AddYears(-1));
        }
    }
}
